package com.healthcare;

import java.io.IOException;
import java.io.PrintWriter;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import webservices.HealthCareServerProxy;

import com.google.gson.Gson;
import com.sun.xml.internal.bind.v2.schemagen.xmlschema.List;

/**
 * Servlet implementation class UserRegistration
 */
public class DoctorDetails extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public DoctorDetails() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String inputDoctorId = request.getParameter("doctor_id");

		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/aimsfirstconsultdb", "root",
					"");
			stmt = con.createStatement();

			String getDocDetailsSql = "SELECT first_name,last_name, email, gender, specialization, clinic_name,"
					+ " clinic_address, clinic_zip,"
					+ "phone, hours, website, about_me, education, experience FROM doctors where doctor_id="
					+ inputDoctorId;

			rs = stmt.executeQuery(getDocDetailsSql);

			// displaying records
			while (rs.next()) {
				String resfname = rs.getObject(1).toString();
				String reslname = rs.getObject(2).toString();
				String resmail = rs.getObject(3).toString();
				String resgender = rs.getObject(4).toString();
				String resspecialization = rs.getObject(5).toString();
				String resclinicname = rs.getObject(6).toString();
				String resclinicaddress = rs.getObject(7).toString();
				String rescliniczip = rs.getObject(8).toString();
				String resphone = rs.getObject(9).toString();
				String reshours = rs.getObject(10).toString();
				String reswebsite = rs.getObject(11).toString();
				String resaboutme = rs.getObject(12).toString();
				String reseducation = rs.getObject(13).toString();
				String resexperience = rs.getObject(14).toString();

				Doctor doctor = new Doctor();
				doctor.setDoctorid(inputDoctorId);
				doctor.setFirstname(resfname);
				doctor.setLastname(reslname);
				doctor.setPhone(resphone);
				doctor.setHours(reshours);
				doctor.setSpecialization(resspecialization);
				doctor.setEmail(resmail);
				doctor.setGender(resgender);
				doctor.setClinicname(resclinicname);
				doctor.setClinicaddress(resclinicaddress);
				doctor.setCliniczip(rescliniczip);
				doctor.setWebsite(reswebsite);
				doctor.setAboutme(resaboutme);
				doctor.setEducation(reseducation);
				doctor.setExperience(resexperience);

				request.setAttribute("doctor", doctor);
			}

			RequestDispatcher dispatcher = request
					.getRequestDispatcher("/DocProfile.jsp");
			dispatcher.forward(request, response);
		} catch (SQLException e) {
			throw new ServletException("Servlet Could not display records.", e);
		} catch (ClassNotFoundException e) {
			throw new ServletException("JDBC Driver not found.", e);
		} finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;
				}
				if (stmt != null) {
					stmt.close();
					stmt = null;
				}
				if (con != null) {
					con.close();
					con = null;
				}
			} catch (SQLException e) {
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String docid = request.getParameter("data");

		String userid =	request.getSession().getAttribute("userid").toString();
			System.out.println("docid inside Article"+docid);
		
			int usId = Integer.parseInt(userid);
			int dcid = Integer.parseInt(docid);
	
			String status = "false";
					
					HealthCareServerProxy proxy = new HealthCareServerProxy();
					if(proxy.deleteDoctors(usId,dcid));
					{
					RequestDispatcher dispatcher = request.getRequestDispatcher("/DocList.jsp");
					System.out.println("inside the delet");
					status="done";
					request.setAttribute("status", status);
					request.getAttribute("spec");
					dispatcher.forward(request, response);
					System.out.println("inside the delet11");
					}

	}
}
